package emp.dao.rowmapper;

import emp.entity.Department;
import emp.entity.Education;
import emp.entity.Employee;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Employee映射类
 */
public class EmployeeRowMapper implements RowMapper<Employee> {

    @Override
    public Employee mapRow(ResultSet rs, int rowNum) throws SQLException {
        Employee employee = new Employee();

        // Mapping basic fields
        employee.setEmpId(rs.getInt("emp_id"));
        employee.setEmpName(rs.getString("emp_name"));
        employee.setGender(rs.getString("gender").charAt(0));  // Assuming gender is stored as a string (e.g. "M" or "F")
        employee.setAge(rs.getInt("age"));
        employee.setSalary(rs.getDouble("salary"));
        employee.setAddress(rs.getString("address"));
        employee.setHometown(rs.getString("hometown"));
        employee.setPhone(rs.getString("phone"));
        employee.setDeptId(rs.getInt("dept_id"));

        // Mapping Education
        Education education = new Education();
        education.setEduId(rs.getInt("edu_id"));
        education.setEduLevel(rs.getString("edu_level"));

        employee.setEducation(education);

        // Mapping Department
        Department department = new Department();
        department.setDeptId(rs.getInt("dept_id"));
        department.setDeptName(rs.getString("dept_name"));
        employee.setDepartment(department);

        return employee;
    }
}
